package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogMessage;
import com.ruoyi.blog.service.IBlogMessageService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excel.ExcelUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 消息管理Controller
 *
 * @author 刘哲贤
 * @date 2022-08-30
 */
@RestController
@RequestMapping("/admin/message")
public class BlogMessageController extends BaseController {
    @Resource
    private IBlogMessageService blogMessageService;

    /**
     * 查询消息管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogMessage blogMessage) {
        startPage();
        List<BlogMessage> list = blogMessageService.selectBlogMessageList(blogMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:message:export')")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogMessage blogMessage) {
        List<BlogMessage> list = blogMessageService.selectBlogMessageList(blogMessage);
        ExcelUtils<BlogMessage> util = new ExcelUtils<BlogMessage>(BlogMessage.class);
        util.exportData(response, list, "消息管理数据");
    }

    /**
     * 获取消息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(blogMessageService.selectBlogMessageById(id));
    }

    /**
     * 新增消息管理
     */
    @PreAuthorize("@ss.hasPermi('admin:message:add')")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogMessage blogMessage) {
        blogMessage.setCreateTime(new Date());
        return toAjax(blogMessageService.insertBlogMessage(blogMessage));
    }

    /**
     * 修改消息管理
     */
    @PreAuthorize("@ss.hasPermi('admin:message:edit')")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogMessage blogMessage) {
        return toAjax(blogMessageService.updateBlogMessage(blogMessage));
    }

    /**
     * 删除消息管理
     */
    @PreAuthorize("@ss.hasPermi('admin:message:remove')")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blogMessageService.deleteBlogMessageByIds(ids));
    }
}
